package com.example.hearthstoneagain;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import android.os.Build;

public class DeckSelectionActivity extends ActionBarActivity {

	Context context;
	
	ListView lst;
	
	List<HashMap<String,String>> listadecks;
	
	String classe;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_deck_selection);
		
		Bundle extras = getIntent().getExtras();
		
		classe = extras.getString("classe");
		
		context = this.getApplicationContext();
		
		listadecks = new ArrayList<HashMap<String,String>>();
		
		// pegar as decks do banco de dados
		
		BancodeDados banco = new BancodeDados(context);
		
		SQLiteDatabase dtb = banco.getReadableDatabase();
		
		String nomecol[] = {"name","custo"};
		
		String args[] = {classe};
		
		Cursor resp_decks = dtb.query("decks", nomecol, "classe = ?", args, null, null, "name ASC");
		
		resp_decks.moveToFirst();
		
		for(int i=0;i<resp_decks.getCount();i++) {
			HashMap<String, String> hm = new HashMap<String,String>();
			hm.put("deck", resp_decks.getString(0));
			int custodedust = resp_decks.getInt(1);
			hm.put("custo", Integer.toString(custodedust));
			hm.put("dust", Integer.toString(R.drawable.dust));
			listadecks.add(hm);
			resp_decks.moveToNext();
		}
		
		dtb.close();
		
		
		//criar o adapter
		
		String[] from = { "dust","custo","deck" };

		int[] to = { R.id.dust,R.id.custo,R.id.deck};
		
		CustomAdapter adapter = new CustomAdapter(getBaseContext(), listadecks, R.layout.listview_layout2, from, to);
		
		lst = (ListView) this.findViewById(R.id.listView3);
		
		lst.setAdapter(adapter);
		
		
		lst.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				
				HashMap<String, String> hm = (HashMap<String, String>) lst.getAdapter().getItem(arg2);
				
				String nomedeck = hm.get("deck");
				
				Intent intent = new Intent (DeckSelectionActivity.this,DeckActivity.class);
				intent.putExtra("deck", nomedeck);
				
				TabGroupActivity parentActivity = (TabGroupActivity) getParent();
				parentActivity.startChildActivity("DeckActivity", intent);
				
				//startActivity(intent);
			}
		});
		
	}
}
